﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using DevExpress.Xpo;
using Zero.Core.Domain;
using Zero.Core.Domain.Query;
using Zero.DataLayer.Donnees;
using Zero.POCO.Support;

namespace Zero.DataLayer.Repository
{
    public class SupportRadioRepository : IReadOnlyRepository<SupportRadio, int>
    {
        public SupportRadio FindBy(int id)
        {

            var config = new AppSettingsReader();
            using (
                var uow = new UnitOfWork()
                {
                    ConnectionString =
                        ((string)config.GetValue("connect", typeof(string)))
                })
            {
                var Instance = uow.GetObjectByKey<XpoSupportRadio>(id);
                if(Instance==null)
                return null;
                return MAP.MapInverse.MapSupportRadio(Instance);
            }
        }

        public IEnumerable<SupportRadio> FindAll()
        {
            List<SupportRadio> Model;

            var config = new AppSettingsReader();
            using (
             var uow = new UnitOfWork()
             {
                 ConnectionString =
                     ((string)config.GetValue("connect", typeof(string)))
             })
            {
                var Instances = new XPCollection<Zero.DataLayer.Donnees.XpoSupportRadio>(uow);
                Model = Instances.Select(Instance => MAP.MapInverse.MapSupportRadio(Instance)).ToList();
            }

            return Model;
        }

        public IEnumerable<SupportRadio> FindBy(Query query)
        {
            List<SupportRadio> Model;
            var firstOrDefault = query.Criteria.FirstOrDefault();
            if (firstOrDefault != null && firstOrDefault.criteriaOperator == CriteriaOperator.Equal)
            {

                var config = new AppSettingsReader();
                using (
               var uow = new UnitOfWork()
               {
                   ConnectionString =
                       ((string)config.GetValue("connect", typeof(string)))
               })
                {
                    var instances = new XPCollection<XpoSupportRadio>(uow);
                    IEnumerable<XpoSupportRadio> Xpoinstances;
                    var orDefault = query.Criteria.FirstOrDefault();
                    Xpoinstances = orDefault != null && orDefault.PropertyName == "Libelle" ? instances.Where(p =>
                    {
                        var criterion = query.Criteria.FirstOrDefault();
                        return criterion != null && p.Libelle == criterion.Value.ToString();
                    }) : instances.Where(p =>
                    {
                        var @default = query.Criteria.FirstOrDefault();
                        return @default != null && p.Code.ToString() == @default.Value.ToString();
                    });
               
                    Model = new List<SupportRadio>();
                    foreach (var individu in Xpoinstances)
                    {
                        Model.Add(MAP.MapInverse.MapSupportRadio(individu));
                    }
                }

                return Model;
            }
            return null;
        }

        public IEnumerable<SupportRadio> FindBy(Query query, int index, int count)
        {
            throw new NotImplementedException();
        }
    }


    public  class RepositoryRadioCache : IReadOnlyRepository<SupportRadio, int>
    {
        private static IEnumerable<SupportRadio> instances;
        public SupportRadio FindBy(int id)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<SupportRadio> FindAll()
        {
            throw new NotImplementedException();
        }

        public  IEnumerable<SupportRadio> FindBy(Query query)
        {
            if (instances == null)
                instances = new SupportRadioRepository().FindAll();


            var firstOrDefault = query.Criteria.FirstOrDefault();
            var list = firstOrDefault != null && firstOrDefault.PropertyName == "Libelle" ? instances.Where(p =>
            {
                var orDefault = query.Criteria.FirstOrDefault();
                return orDefault != null && p.Libelle == orDefault.Value.ToString();
            }) : instances.Where(p =>
            {
                var criterion = query.Criteria.FirstOrDefault();
                return criterion != null && p.Code.ToString() == criterion.Value.ToString();
            }); 
            return list;
        }

        public IEnumerable<SupportRadio> FindBy(Query query, int index, int count)
        {
            throw new NotImplementedException();
        }
    }
}
